/*
 * @Author: szx
 * @Date: 2022-06-12 19:50:11
 * @LastEditTime: 2022-06-12 20:09:14
 * @Description:
 * @FilePath: \leetcode\面试题\16\25\25.js
 */
/**
 * @param {number} capacity
 */
var LRUCache = function (capacity) {
    this.cache = new Map();
    this.capacity = capacity;
};

/**
 * @param {number} key
 * @return {number}
 */
LRUCache.prototype.get = function (key) {
    const cache = this.cache;
    if (cache.has(key)) {
        const value = cache.get(key);
        cache.delete(key);
        cache.set(key, value);
        return value;
    } else return -1;
};

/**
 * @param {number} key
 * @param {number} value
 * @return {void}
 */
LRUCache.prototype.put = function (key, value) {
    const cache = this.cache;
    if (cache.has(key)) cache.delete(key);
    if (cache.size == this.capacity) cache.delete(cache.keys().next().value);
    cache.set(key, value);
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * var obj = new LRUCache(capacity)
 * var param_1 = obj.get(key)
 * obj.put(key,value)
 */
